﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using Manager.Common;
using Manager.ViewModels;
using System.Linq;

namespace Manager.FormAdd_Edit
{
    public partial class frm_CheckOutDetail : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        public List<DangKyPhongViewModel> lstDangKyPhong;
        public bool isClose = true;
        public double TongTienVND = 0;
        public double TongTien = 0;

        public frm_CheckOutDetail()
        {
            InitializeComponent();
        }

        public frm_CheckOutDetail(DangKyPhongViewModel _vmDangKyPhong, string gioVao, string gioRa)
        {
            InitializeComponent();
            lstDangKyPhong = new List<DangKyPhongViewModel>();
            txtMaPhong.Text = _vmDangKyPhong.MaPhong;
            txtGioVao.Text = gioVao;
            txtGioRa.Text = gioRa;
            var soNgayO = (_vmDangKyPhong.dateNgayTra - _vmDangKyPhong.dateNgayDen).Days;
            if (soNgayO <= 1)
                lstDangKyPhong.Add(_vmDangKyPhong);
            else
            {
                for (int i = 0; i < soNgayO; i++)
                {
                    var vm = new DangKyPhongViewModel();
                    vm.TenKhachHang = _vmDangKyPhong.TenKhachHang;
                    vm.strGiaPhong = _vmDangKyPhong.strGiaPhong;
                    vm.GiaPhong_DonViTienTe = _vmDangKyPhong.GiaPhong_DonViTienTe;
                    vm.dateNgayDen = _vmDangKyPhong.dateNgayDen.AddDays(i);
                    vm.NgayDen = vm.dateNgayDen.ToString("dd/MM/yyyy");
                    vm.dateNgayDi = _vmDangKyPhong.dateNgayDen.AddDays(i + 1);
                    vm.NgayDi = vm.dateNgayDi.ToString("dd/MM/yyyy");

                    lstDangKyPhong.Add(vm);
                }
            }
        }

        private void frm_CheckOutDetail_Load(object sender, EventArgs e)
        {
            gridListRoom.DataSource = null;
            gridListRoom.DataSource = lstDangKyPhong;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            isClose = true;
            this.Close();
        }

        private void butCapNhat_Click(object sender, EventArgs e)
        {
            isClose = false;
            TongTien = 0;
            var tienTe = db.TienTes.SingleOrDefault(t => t.MaDonVi == lstDangKyPhong[0].GiaPhong_DonViTienTe);
            foreach (var viewmodel in lstDangKyPhong)
            {
                TongTien += viewmodel.GiaPhong;
                viewmodel.strGiaPhong = CommonFunction.AddSeparator(viewmodel.GiaPhong);
            }
            TongTienVND = TongTien * tienTe.TyGiaThamKhao.Value;
            this.Close();
        }        
    }
}